package com.baizan.daka.ui.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import com.baizan.daka.util.AppUtils
import com.dianzan.daka.R

class ButtomDialog : Dialog {
    private var mListener: OnItemSelectListener? = null
    private var mLvSelections: ListView? = null

    var mSelection: Array<String>? = null

    val defaultPadding = 15
    val defaultTextSize = 18

    @ColorRes
    val defaultColor = R.color.app_text_blue


    constructor(context: Context, themeResId: Int) :
            super(context, themeResId)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.setContentView(R.layout.dialog_selections)
        mLvSelections =
            findViewById<View>(R.id.lvSelections) as ListView
        findViewById<View>(R.id.cancel_tv).setOnClickListener { dismiss() }
        val selectionAdapter = SelectionAdapter(context, mSelection, this)
        mLvSelections?.setAdapter(selectionAdapter)
        mLvSelections?.setOnItemClickListener(AdapterView.OnItemClickListener { parent, view, position, id ->
            mListener?.let {
                it.OnItemSelected(
                    this@ButtomDialog,
                    position,
                    mSelection?.get(position)
                )
            }

        })
        val dialogWindow = this.window
        dialogWindow.setGravity(Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL)
    }

    fun setListener(listener: OnItemSelectListener) {
        mListener = listener
    }

    interface OnItemSelectListener {
        fun OnItemSelected(dialog: Dialog, position: Int, title: String?)
    }

    open fun newTextView(
        context: Context,
        padd: Int,
        textSize: Int,
        @ColorRes color: Int
    ): TextView? {
        val textView = TextView(context)
        val padding: Int =
            AppUtils.dp2px(if (padd == 0) defaultPadding else padd)
        textView.setPadding(padding, padding, padding, padding)
        textView.setTextSize(
            TypedValue.COMPLEX_UNIT_DIP,
            if (textSize == 0) defaultTextSize.toFloat() else textSize.toFloat()
        )
        textView.setTextColor(context.resources.getColor(color))

        textView.gravity = Gravity.CENTER
        return textView
    }

    class SelectionAdapter : ArrayAdapter<String> {
        var dialog: ButtomDialog

        constructor(context: Context, selections: Array<String>?, dialog: ButtomDialog) :
                super(context, 0, selections) {
            this.dialog = dialog
        }


        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
            val textView = dialog.newTextView(
                context,
                dialog.defaultPadding,
                dialog.defaultTextSize,
                dialog.defaultColor
            )
            textView?.text = getItem(position)
            return textView
        }
    }

    class Builder(private val context: Context) {

        private var contents: Array<String>? = null
        private var listener: OnItemSelectListener? = null


        fun setContentData(data: Array<String>): Builder {
            contents = data
            return this
        }

        fun setListener(listener: OnItemSelectListener?): Builder {
            this.listener = listener
            return this
        }


        fun Build(): ButtomDialog {
            val dialog = ButtomDialog(context, R.style.DefaultDialog)
            dialog.mListener = listener
            dialog.mSelection = contents
            return dialog
        }

    }
}